package com.kangaroo.myhome.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.kangaroo.myhome.mapper.TInspVideoMapper;
import com.kangaroo.myhome.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kangaroo.myhome.utils.SecurityUtils;
import com.kangaroo.myhome.vo.ResponseModel;

import tk.mybatis.mapper.util.StringUtil;

@Service
public class TInspBigFileUploadService {

    @Autowired
    private TInspVideoMapper tInspVideoMapper;

    /**
     * 
     * @param projectId
     * @param page
     * @param rows
     * @return
     */
    public ResponseModel findAll(String projectId, int page, int rows) {
        ResponseModel model = new ResponseModel();

        CommonSearch search = new CommonSearch();
        search.setString1(projectId);
        search.setPagination(page, rows);
        List<TInspVideo> users = tInspVideoMapper.selectByPagination(search);
        long count = tInspVideoMapper.selectByPaginationCount(search);
        model.setCode(200);
        model.setMessage("数据获取成功");
        model.setDataDataGrid(users, count);
        return model;
    }

    /**
     * @param tInspVideo
     * @return
     */
    public ResponseModel edit(TInspVideo tInspVideo) {

        ResponseModel model = new ResponseModel();

        if (tInspVideo == null) {
            model.setCode(201);
            model.setMessage("参数异常");
            return model;
        }
        int result = 0;
        String id = tInspVideo.getId();
        System.out.println("id#tInspVideo:" + id + "#" +tInspVideo.getProjectName());
        if (StringUtil.isEmpty(id)) {
            tInspVideo.setCreateTime(new Date());
            tInspVideo.setId(UUID.randomUUID().toString());
            tInspVideo.setCreator(SecurityUtils.getUsername());
            result = tInspVideoMapper.insertSelective(tInspVideo);
        }else{
            result = tInspVideoMapper.updateByPrimaryKeySelective(tInspVideo);
        }
        model.setCode(200);
        model.setMessage("操作成功");
        return model;
    }

    /**
     * @param tInspVideo
     * @return
     */
    public TInspVideo view(TInspVideo tInspVideo) {
        ResponseModel model = new ResponseModel();
        TInspVideo result = tInspVideoMapper.selectByPrimaryKey(tInspVideo.getId());
        return result;
    }

    public void delete(TInspVideo tInspVideo) {
        String ids = tInspVideo.getId();
        String[] idArr = ids.split(",");
        for(String id : idArr) {
            tInspVideoMapper.deleteByPrimaryKey(id);
        }
    }

}
